<?php

namespace app\admin\controller\qingdongwms\statistic;



use addons\qingdongwms\model\Receivables;
use app\admin\model\AuthGroup;
use app\common\controller\Backend;
use addons\qingdongwms\model\Contract as ContractModel;
use addons\qingdongwms\model\Product as ProductModel;
use addons\qingdongwms\model\ContractProduct;
use addons\qingdongwms\model\Staff;
use fast\Tree;
use think\Config;

/**
 * 产品分析
 */
class Product extends Backend
{
    public function _initialize()
    {
        parent::_initialize();

        $childrenGroupIds = $this->auth->getChildrenGroupIds(true);
        $groupList = collection(AuthGroup::where('id', 'in', $childrenGroupIds)->select())->toArray();

        Tree::instance()->init($groupList);
        $groupList = [];
        if ($this->auth->isSuperAdmin()) {
            $groupList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
        } else {
            $groups = $this->auth->getGroups();
            $groupIds = [];
            foreach ($groups as $m => $n) {
                if (in_array($n['id'], $groupIds) || in_array($n['pid'], $groupIds)) {
                    continue;
                }
                $groupList = array_merge($groupList, Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['pid'])));
                foreach ($groupList as $index => $item) {
                    $groupIds[] = $item['id'];
                }
            }
        }
        $this->assign('groupdata',$groupList);
    }

    /**
     * 产品销售金额
     */
    public function money()
    {
        $type=input('type','bar');//bar 柱状图 pie 饼状图
        $row = input('row/a');
        $year = $row['year'] ?? date('Y');
        $between = [strtotime($year . '-01-01'), strtotime($year . '-12-31') + 86400 - 1];
        $group_id = $row['group_id'] ?? 0;
        $ids=[];
        if ($group_id) {//角色组
            $ids = Staff::getGroupStaffIds($group_id);
        }
        if (isset($row['staff_id']) && $row['staff_id']) {
            $ids = $staff_id = $row['staff_id'];
        } else {
            $staff_id = '';
        }
        $where = ['createtime' => [
            'between',
            $between
        ]];

        $contracts = ContractProduct::where($where)->field("FROM_UNIXTIME(createtime,'%Y-%m') as ctime,sum(number) as number,sum(price) as price")->group('ctime')->select();
        $select = [];
        $lastselect = [];
//dump();exit;
        foreach ($contracts as $v) {
            $select[$v['ctime']] = $v['number'];
            $lastselect[$v['ctime']] = $v['price'];
        }
        $data=[];
        $time = $between[0];
        $toMonthTime = $between[1];
        for ($time; $time <= $toMonthTime; $time = strtotime('+1 month', $time)) {
            $data['date'][] = intval(date('m', $time)).'月份';
            $data['number'][] = $select[date('Y-m', $time)] ?? 0;
            $data['price'][] = $lastselect[date('Y-m', $time)] ?? 0;
        }
        $data['title']='产品销量统计分析';
        $this->view->assign([
            'data' =>$data,
            'year' => $year,
            'staff_id' => $staff_id,
            'group_id' => $group_id,
            'type' => $type,
        ]);
        return $this->view->fetch();
    }

    /**
     * 产品排行TOP10
     */
    public function top()
    {
        $type=input('type','bar');//bar 柱状图 pie 饼状图
        $row = input('row/a');
        $year = $row['year'] ?? date('Y');
        $between = [strtotime($year . '-01-01'), strtotime($year . '-12-31') + 86400 - 1];
        $where = ['createtime' => [
            'between',
            $between
        ]];
        $data=[];
        $product=[];
        //产品数
        $productId = ContractProduct::where($where)->group('product_id')->limit(10)->column('product_id');
        foreach($productId as $k=>$v) {
            $num = ContractProduct::where(['product_id'=>$v])->sum('number');
            $name = ProductModel::where(['id'=>$v])->value('name');
            if(!$name){
                $name = ProductModel::onlyTrashed()->where(['id'=>$v])->value('name');
            }
            $product[] = array(
                'name'=>$name,
                'number'=>(int)$num ?? 0
            );

        }
        if($product){
            $b = array_column($product, 'number');
            array_multisort($b, SORT_DESC, $product);
            foreach($product as $k=>$vs){
                $data['name'][] = $vs['name'];
                $data['number'][] = $vs['number'] ?? 0;
            }
        }

        $data['title']='产品销量排行Top10';
        $this->view->assign([
            'data' =>$data,
            'year' => $year,
            'type' => $type,
        ]);
        return $this->view->fetch();
    }

}
